let array = [[1,2,3],[8,9,4],[7,6,5]]
var spiralArray = function(array) {
    if(!array.length || !array[0].length){
        return []
    }
    let rows = array.length, colums = array[0].length
    let total = rows * colums
    let voval = new Array(rows).fill(0).map(() => new Array(colums).fill(false));
    let luxian = [[0,1],[1,0],[0,-1],[-1,0]]
    let row = 0,colum=0,luxianindex = 0
    let result = new Array(total).fill(0)
    for(let i=0;i<total;i++){
         result[i] = array[row][colum]
         voval[row][colum] = true
        const nextrow = row + luxian[luxianindex][0],nextcolum = colum + luxian[luxianindex][1]
        if(!(nextrow>=0&&rows>nextrow&&nextcolum>=0&&colums>nextcolum&&!(voval[nextrow][nextcolum]))){
            luxianindex=(luxianindex+1)%4
        }
        row+=luxian[luxianindex][0]
        colum+=luxian[luxianindex][1]
    }
    return result
};
console.log(spiralArray(array));
